package com.infore.airsupervise.ui.activity;

import android.Manifest;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.QuestionE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.ui.widget.EmptyView;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 人员详情界面
 */
public class PersonDetailActivity extends BaseTitleBarActivity {


    @BindView(R.id.tv_pson_name)
    TextView tvPsonName;
    @BindView(R.id.tv_pson_address)
    TextView tvPsonAddress;
    @BindView(R.id.ll_pson_detail_list)
    LinearLayout llPsonDetailList;
    @BindView(R.id.progress_finish)
    DonutProgress progressFinish;
    @BindView(R.id.progress_delay)
    DonutProgress progressDelay;
    @BindView(R.id.progress_problem)
    DonutProgress progressProblem;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.emptyView)
    EmptyView emptyView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;

    private UserE userE;

    private BaseQuickAdapter<QuestionE, BaseViewHolder> adapter;
    private List<QuestionE> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_detail);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (null != it) {
            userE = (UserE) it.getSerializableExtra(Constants.INTENT_OBJ_KEY);
        }
        title.setText("人员信息");
        rightMenu.setVisibility(View.INVISIBLE);
        intPsonDetaiUI();
        initRecycle();
    }

    private void intPsonDetaiUI() {
        try {
            tvPsonAddress.setText("当前位置：" + userE.getAddress());
            tvPsonName.setText("网格负责人：" + userE.getRealName());
            tvPhone.setText("联系方式：" + userE.getPhone());
            ObjectAnimator oa_finish = ObjectAnimator.ofObject(progressFinish, "progress", new IntEvaluator(), 0, 66);
            oa_finish.setDuration(1000)
                    .setInterpolator(new AccelerateDecelerateInterpolator());
            oa_finish.start();

            ObjectAnimator oa_delay = ObjectAnimator.ofObject(progressDelay, "progress", new IntEvaluator(), 0, 23);
            oa_delay.setDuration(1000)
                    .setInterpolator(new AccelerateDecelerateInterpolator());
            oa_delay.start();
        } catch (NullPointerException ne) {
            ne.printStackTrace();
        }

    }


    /**
     * 本月上报信息列表recycle
     */
    private void initRecycle() {
        emptyView.setShowInfo(R.drawable.person_no_data, MsgConst.EMPTY_DATA);
        swipeLayout.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        //适配器
        adapter = new BaseQuickAdapter<QuestionE, BaseViewHolder>(R.layout.item_person_inspect, datas) {
            @Override
            protected void convert(BaseViewHolder helper, QuestionE item) {

            }
        };
        //点击事件
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.tv_pson_sms, R.id.tv_pson_email, R.id.tv_pson_call ,R.id.emptyView})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pson_sms: //短信
                if (TextUtils.isEmpty(userE.getPhone())) {
                    ToastUtil.showShort(ctx, "号码为空,不能发送短信");
                    return;
                }
//                Intent itSMS = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.parse("smsto:"+userE.getPhone());
                Intent itSMS = new Intent(Intent.ACTION_SENDTO,uri);
//                itSMS.putExtra("address", userE.getPhone());
//                itSMS.setType("vnd.android-dir/mms-sms");
                startActivity(itSMS);

                break;
            case R.id.tv_pson_email: // 邮件
                ToastUtil.showShort(ctx, "无邮箱地址，不能发送邮件");
                break;
            case R.id.tv_pson_call: //电话
                Intent itCall = new Intent(Intent.ACTION_CALL);
                String tel = "tel:";
                if (TextUtils.isEmpty(userE.getPhone())) {
                    ToastUtil.showShort(ctx, "号码为空，不能拨打电话");
                    return;
                }
                tel += userE.getPhone();
                Uri uri2 = Uri.parse(tel);
                itCall.setData(uri2);

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(itCall);
                break;
            case R.id.emptyView :
                break;
        }
    }



}
